//
//  YSLBarButtonItem+Extension.swift
//  SinaWeibo
//
//  Created by 嵩雷 on 16/3/6.
//  Copyright © 2016年 嵩雷. All rights reserved.
//

import UIKit

private let ItemNormalColor = UIColor(red: 80/255, green: 80/255, blue: 80/255, alpha: 1)
private let ItemHighlightedColor = UIColor.orangeColor()
private let ItemFontSize: CGFloat = 14

extension UIBarButtonItem {
    
    convenience init(imgName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector) {
        self.init()
        let button = UIButton()
        button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        
        if let img = imgName {
            button.setImage(UIImage(named: img), forState: .Normal)
            button.setImage(UIImage(named: "\(img)_highlighted"), forState: .Highlighted)
        }
        if let t = title {
            button.setTitle(t, forState: .Normal)
//            Type 'UIFont' has no member 'ItemFontSize'
//            Cannot assign value of type 'CGFloat' to type 'UIFont!'
            button.titleLabel?.font = UIFont.systemFontOfSize(ItemFontSize)
            button.setTitleColor(ItemNormalColor, forState: .Normal)
            button.setTitleColor(ItemHighlightedColor, forState: .Highlighted)
        }
        button.sizeToFit()
        customView = button
    }
}
